package com.exsun.ex1077.ui.activity.mileage

import android.os.Bundle
import com.drake.brv.PageRefreshLayout
import com.drake.brv.utils.divider
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.ActivityMileageListBinding
import com.exsun.ex1077.network.entity.MileageRank
import com.exsun.ex1077.ui.popup.ListMenuPopup
import com.lxj.xpopup.XPopup
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.callback.CommonCallback
import com.zhangping.fastjetpack.extend.*
import com.zhangping.fastjetpack.extend.base.parseState
import com.zhangping.fastjetpack.extend.view.afterTextChange
import com.zhangping.fastjetpack.model.Item
import com.zhangping.fastjetpack.model.Tree
import kotlinx.android.synthetic.main.activity_mileage_list.*

/**
 * 描述：里程统计列表
 * 作者：ZhangPing
 * 创建时间：2023/3/27 9:50
 */
class MileageListActivity: BaseActivity<MileageListViewModel, ActivityMileageListBinding>() {

    override fun title() = "里程统计"
    override fun barBgColor() = R.color.transparent

    private lateinit var listMenuPopup: ListMenuPopup

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        listView.linear().divider {
            endVisible = true
            setDivider(1, true)
            setColor(R.color.divider.toColorRes())
        }.setup {
            addType<String> { R.layout.item_mileage_title }
            addType<MileageRank> { R.layout.item_mileage_content }
        }
        listMenuPopup = XPopup.Builder(this)
            .atView(searchView)
            .isLightStatusBar(true)
            .isViewMode(true)
            .asCustom(ListMenuPopup(this, true)) as ListMenuPopup
        listMenuPopup.setOnConfirmListener(object : CommonCallback{
            override fun onCall() {
                mViewModel.apply {
                    startTime = listMenuPopup.getStartDate().toDateStr()
                    endTime = listMenuPopup.getEndDate().toDateStr()
                    deptIdList = listMenuPopup.getDeptIdList()
                    manageTypeIdList = listMenuPopup.getManageTypeList()
                }
                refreshLayout.autoRefresh()
            }
        })
        mViewModel.startTime = listMenuPopup.getStartDate().toDateStr()
        mViewModel.endTime = listMenuPopup.getEndDate().toDateStr()
        listMenu.setOnClickListener {
            listMenuPopup.show()
        }
        searchEdit.afterTextChange {
            refreshLayout.queueRefresh(false)
        }
        refreshLayout.onRefresh {
            mViewModel.loadData(index)
        }.autoRefresh()
        refreshLayout.onLoadMore {
            mViewModel.loadData(index)
        }
        mViewModel.apply {
            loadDeptTree()
            loadManageTypeList()
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.apply {
            deptTreeResult.observe(this@MileageListActivity){
                parseState(it, {
                    Tree.updateTreeLevel(it)
                    postDelayed(100){
                        listMenuPopup.setCompanyTreeData(it)
                    }
                })
            }
            objectTypeResult.observe(this@MileageListActivity){
                parseState(it, {
                    listMenuPopup.setManageTypeData(it.map { Item(it.key, it.value) })
                })
            }
            dataLiveData.observe(this@MileageListActivity){
                parseState(it, {
                    val list = it.rows ?: emptyList()
                    //对数据进行分组
                    val newList = arrayListOf<Any>()
                    val srcList = if (refreshLayout.index == PageRefreshLayout.startIndex || listView.models.isNullOrEmpty()) emptyList() else listView.models!!
                    list.forEach {
                        val date = it.day.split(" ")[0]
                        if (!srcList.contains(date) && !newList.contains(date)){
                            newList.add(date)
                        }
                        newList.add(it)
                    }
                    refreshLayout.queueAddData(newList){ mViewModel.pageSize == list.size }
                }, {
                    refreshLayout.queueRefreshException()
                })
            }
        }
    }

}